const Mixins = {}
let wraper = require('../component/wraper.vue')
let head = require('../component/head.vue')
let button = require('../component/button.vue')
let page = require('../component/page.vue')
let MyDiv = require('../component/my_div.vue')
let MyText = require('../component/my_text.vue')
const nav = weex.requireModule('navigator')
const zh = require('../lang/zh.js')
const pref = weex.requireModule("pref")
const net = require('../utils/http.js')
Mixins.install = (Vue, options) => {
  Vue.mixin({
    components: {
      wraper,
      head,
      button,
      page,
      MyDiv,
      MyText
    },
    data(){
      return {
        modal: {},
        navigator: {},
        loading: false,
        locale: '',
        lang:'',
        theme: '',
        placeholderColor: '#999999',
        inputText:{
          color:'#333'
        },
        current_user: {},
        current_wallet:{}

      }
    },
    watch: {
      locale(val) {
        pref&&pref.setString('locale', val)
        this.lang = require(`../lang/${val}.js`)
        this.$forceUpdate()
      }
    },
    methods: {
      toast(msg)
      {
        if (this.modal && this.modal.toast)
          this.modal.toast({
            message: msg,
          })
      },
      alert(msg)
      {
        this.modal.alert({message: msg,okTitle: '确定'})
      },
      push(url, param)
      {
        if (!param)
          this.navigator.push(url)
        else
          this.navigator.pushParam(url, param)
      },
      get_theme() {
        // return 'night'
        let st = weex.requireModule('static')
        let theme = st.getString('theme')
        if (theme) {
          return theme;
        }
        let pref = weex.requireModule('pref')
        theme = pref.getString('theme')
        if (theme) {
          st.setString('theme', theme)
          return theme;
        }
        theme = 'day'
        st.setString('theme', theme)
        pref.setString('theme', theme)
        return theme;

      },
      getScreenHeight() {
        return 750 / weex.config.env.deviceWidth * weex.config.env.deviceHeight
      },
      $t(key) {
        if (!key) return ''
        const keys = key.split('.')
        return this.lang[keys[0]] && this.lang[keys[0]][keys[1]] || ''
      },
      get_current_user(){
        const ac = pref && pref.get('account') || {}
        const current_user_key = pref && pref.getString('current_user_key')
        if (Object.keys(ac).length > 0) {
          const curr_ac = ac[current_user_key]
          return curr_ac && curr_ac.user
        } else {
          return {}
        }
      },
      get_current_wallet(){
        const ac = pref && pref.get('account') || {}
        const current_user_key = pref && pref.getString('current_user_key')
        if (Object.keys(ac).length > 0) {
          const curr_ac = ac[current_user_key]
          return curr_ac && curr_ac.wallet
        } else {
          return {}
        }
      },
      set_current_user(user){
         let ac = pref && pref.get('account') || {}
         const current_user_key = pref && pref.getString('current_user_key')
          if (Object.keys(ac).length > 0) {
            ac[current_user_key]['user'] = user
            pref.set('account',ac)
          } else {
            return {}
          }
      }
    },
    created()
    {
      const st = weex.requireModule("static")
      this.current_user = this.get_current_user()
      this.current_wallet = this.get_current_wallet()
      this.navigator = nav
      this.modal = weex.requireModule('modal')
      this.locale = pref&&pref.getString('locale') || 'zh'
      this.theme = st&&st.getString('theme') || 'day'
      this.$http = net
      this.pref = pref
      var globalEvent = weex.requireModule('globalEvent');
      globalEvent.addEventListener("onPageInit", (param) => {
        if (WXEnvironment.platform === 'android') {
          let keyboard = weex.requireModule('keyboard')
          keyboard.setKeyboardMode('adjust_pan')
        }
     
        if (this.theme === 'day'){
          this.placeholderColor = '#999999'
          this.inputText ={
            color: '#333333'
          }
        }else {
           this.placeholderColor = '#999999'
           this.inputText = {
             color: '#ffffff'
           }
        }
        let p = param
        if (param && p.param && p.bubbles)
          p = p.param
        if (this.onLoad != undefined)
          this.onLoad(p)
      });
    }

  })
}
const install = Mixins.install
export {install}
export  default Mixins
